package com.common.result;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {

    private static final Integer DEFAULT_SUCCESS_CODE = 200;
    private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";
    private static final Integer DEFAULT_ERROR_CODE = 500;
    private static final String DEFAULT_ERROR_MESSAGE = "操作失败";

    private Integer code;
    private String message;
    private T data;

    public static  <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(DEFAULT_SUCCESS_CODE);
        result.setMessage(DEFAULT_SUCCESS_MESSAGE);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok() {
        return new Result<T>(DEFAULT_SUCCESS_CODE, DEFAULT_SUCCESS_MESSAGE, null);
    }

    public static <T> Result<T> error(String message) {
        Result<T> result = new Result<>();
        result.setCode(DEFAULT_ERROR_CODE);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error() {
        return new Result<T>(DEFAULT_ERROR_CODE, DEFAULT_ERROR_MESSAGE, null);
    }
}
